package cn.sfa.controller.product;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.sfa.common.pagination.Pagination;
import cn.sfa.common.taglib.CommonCache;
import cn.sfa.common.utils.CommonUtil;
import cn.sfa.common.utils.DateUtil;
import cn.sfa.common.utils.WebInitBinderContoller;
import cn.sfa.entity.affixItem.SaleAffixItem;
import cn.sfa.entity.manage.FastProductInfo;
import cn.sfa.entity.manage.SaleProduct;
import cn.sfa.entity.system.BranchSysCode;
import cn.sfa.entity.system.ProviderSysCode;
import cn.sfa.entity.users.SaleUsersLogin;
import cn.sfa.service.affix.AffixService;
import cn.sfa.service.product.ProductService;

/**
 *@author 周友平
 *@version 创建时间：2012-4-5上午09:41:42 类说明
 */

@Controller
@RequestMapping("routine/product/index")
public class ProductController extends WebInitBinderContoller {

	@Autowired
	private ProductService productservice;

	@Autowired
	AffixService affixService;

	@Autowired
	private AffixService affixservice;

	@RequestMapping
	public String index(
			@RequestParam(value = "productCode", required = false) String productCode,// 产品代码
			@RequestParam(value = "productTitle", required = false) String productTitle,// 产品名称
			@RequestParam(value = "currentPage", required = false) String currentPage,// 页码
			@RequestParam(value = "provides", required = false) String provides,// 供应商
			@RequestParam(value = "salestatus", required = false) String salestatus,// 
			@RequestParam(value = "channeltype", required = false) String channeltype,// 
			HttpServletRequest request, ModelMap map,
			HttpServletResponse response) {
		// 解决传值乱码
		try {
			request.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String providername = "";
		String providerStr = "";
		if(provides != null){
			providername = provides.substring(provides.indexOf(":")+1,provides.length());
			providerStr = providername ;
			if(providername.lastIndexOf("(") > -1){
				providerStr = providername.substring(0,providername.lastIndexOf("("));
				}
			}

		SaleUsersLogin user = (SaleUsersLogin) request.getSession()
				.getAttribute("user");

		String currentBranchId = user.getBranchId();
		int pageIndex;
		try {
			pageIndex = Integer.valueOf(currentPage);
		} catch (Exception ex) {
			pageIndex = 1;
		}
		
		String url = request.getContextPath() + "/routine/product/index?"
				+ "productCode=" + CommonUtil.jugeAndFixNull(productCode)
				+ "&salestatus=" + CommonUtil.jugeAndFixNull(salestatus)
				+ "&channeltype=" + CommonUtil.jugeAndFixNull(channeltype)
				+ "&providerStr=" + CommonUtil.jugeAndFixNull(providerStr)
				+ "&productTitle=" + CommonUtil.jugeAndFixNull(productTitle);

		Pagination pagination = productservice.SeachSaleProduct(pageIndex, 10,
				url, productCode, productTitle, providerStr, currentBranchId,channeltype,salestatus);
		List<BranchSysCode> list = null;

		map.put("BranchList", list);
		map.put("salestatus", salestatus);
		map.put("channeltype", channeltype);
		map.put("providerStr", providerStr);
		map.put("productCode", productCode);
		map.put("provides", provides);
		map.put("productTitle", productTitle);
		map.put("pagination", pagination);
		return "routine/product/index";
	}

	/*
	 * 产品详细
	 */
	@RequestMapping("/detail/{productid}")
	public String detail(@PathVariable("productid") String productId,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap map) {


		List<SaleAffixItem> affixList1 = productservice.getAffix("SALEPRODUCT1",productId);
		List<SaleAffixItem> affixList2 = productservice.getAffix("SALEPRODUCT2",productId);
		List<SaleAffixItem> affixList3 = productservice.getAffix("SALEPRODUCT3",productId);
		List<SaleAffixItem> affixList4 = productservice.getAffix("SALEPRODUCT4",productId);
		List<SaleAffixItem> affixList5 = productservice.getAffix("SALEPRODUCT2",productId);
		List<SaleAffixItem> affixList6 = productservice.getAffix("SALEPRODUCT6",productId);
		List<SaleAffixItem> affixList7 = productservice.getAffix("SALEPRODUCT7",productId);
		List<SaleAffixItem> affixList8 = productservice.getAffix("SALEPRODUCT8",productId);
		
		map.put("affixList1", affixList1);
		map.put("affixList2", affixList2);
		map.put("affixList3", affixList3);
		map.put("affixList4", affixList4);
		map.put("affixList5", affixList5);
		map.put("affixList6", affixList6);
		map.put("affixList7", affixList7);
		map.put("affixList8", affixList8);
		FastProductInfo productInfo = productservice
				.getFastProductByID(productId);

		SaleProduct saleproduct = productservice
				.getSaleProductByproductId(productId);

		// 查询这个产品是否已经发布过了
		boolean isPublished = productservice.getIsPublished(productId);
		map.put("isPublished", isPublished);
		map.put("saleproduct", saleproduct);
		map.put("productInfo", productInfo);
		return "routine/product/productdetail";
	}

}
